//
//  TFNewFeatureViewController.swift
//  weiboSina
//
//  Created by admin on 15/12/30.
//  Copyright © 2015年 admin. All rights reserved.
//

import UIKit
import SnapKit
class TFNewFeatureViewController: UIViewController ,UIScrollViewDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()

       scrollView.delegate = self
        
        view .addSubview(scrollView)
        
         view.addSubview(pageControll)
        setupUI()
        
    }
    
    func setupUI(){
    
      
        
        pageControll.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-80)
            make.centerX.equalTo(view.snp_centerX)
        }
        
        for i in 0..<4{
        
        
            let imageView: UIImageView = UIImageView(frame: CGRect(x: CGFloat(i) * SCREEN_width, y: 0, width: SCREEN_width, height: SCREEN_height))
            let imageName: String = String(format: "new_feature_\(i+1)", arguments: [])
            imageView.image = UIImage(named: imageName)
            
            if i == 3 {
                
                
            imageView.userInteractionEnabled = true
                
                enterButton.center = CGPoint(x: imageView.frame.width * 0.5, y: imageView.frame.height - 150)
                sharedButton.center = CGPoint(x: enterButton.center.x, y: CGRectGetMinY(enterButton.frame) - 30)
                
            imageView.addSubview(enterButton)
                imageView.addSubview(sharedButton)
                
            
            }
            
      scrollView.addSubview(imageView)
            
        }
    
        scrollView.contentSize = CGSize(width: 4*SCREEN_width, height: 0)
    }
    
    /// 设置最后一页的内容
    ///
    /// - parameter lastPage: 最后一页的view
    private func setupLastPage(lastPage: UIView){
        // lastPage 开启用户交互
        lastPage.userInteractionEnabled = true
        enterButton.center = CGPoint(x: lastPage.frame.width * 0.5, y: lastPage.frame.height - 150)
        sharedButton.center = CGPoint(x: enterButton.center.x, y: CGRectGetMinY(enterButton.frame) - 30)
        
        // 添加子控件
        lastPage.addSubview(enterButton)
        lastPage.addSubview(sharedButton)
        
    }

    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let page = scrollView.contentOffset.x/SCREEN_width+0.5
        pageControll.currentPage = Int(page)
        
    }
    

    lazy var scrollView: UIScrollView = {
    
    let s = UIScrollView(frame: CGRect(x: 0, y: 0, width: SCREEN_width, height: SCREEN_height))
        s.pagingEnabled = true
        
        s.bounces = false
        s.showsHorizontalScrollIndicator = false
    return s
    }()
    
    
    lazy var pageControll: UIPageControl = {
    
    
    let pageC = UIPageControl()
            pageC.currentPageIndicatorTintColor = UIColor.redColor()
        pageC.pageIndicatorTintColor = UIColor.greenColor()

        pageC.numberOfPages = 4
        return pageC
    }()
    // 进入按钮
    private lazy var enterButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "enterButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        // 设置不同状态的图片
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setTitle("进入微博", forState: UIControlState.Normal)
        // 内容多大,按钮多大
        button.sizeToFit()
        return button
    }()
    
    // MARK: - 按钮点击事件
    
    
    @objc private func sharedButtonClick(button: UIButton){
        button.selected = !button.selected
    }
    
    @objc private func enterButtonClick(){
        // 执行进入微博(切换界面)
        print("enterButtonClick")
        NSNotificationCenter.defaultCenter().postNotificationName(HMSwitchRootViewControllerNotification, object: nil)
    }
 
    // 进入按钮
    private lazy var sharedButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "sharedButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        // 设置不同状态的图片
        button.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        button.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        button.setTitle(" 分享到微博", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        // 内容多大,按钮多大
        button.sizeToFit()
        return button
    }()

    
}
